package org.flywaydb.core.internal.database.dm;

import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;

import java.sql.SQLException;

/**
 * @author ZengShuo
 * @description: DMTable
 * @since 2021/8/26 14:08
 */
public class DMTable extends Table<DMDatabase, DMSchema> {
    /**
     * Creates a new table.
     *
     * @param jdbcTemplate The Jdbc Template for communicating with the DB.
     * @param database     The database-specific support.
     * @param schema       The schema this table lives in.
     * @param name         The name of the table.
     */
    public DMTable(JdbcTemplate jdbcTemplate, DMDatabase database, DMSchema schema, String name) {
        super(jdbcTemplate, database, schema, name);
    }

    @Override
    protected void doDrop() throws SQLException {
        jdbcTemplate.execute("DROP TABLE " + database.quote(schema.getName(), name));
    }

    @Override
    protected boolean doExists() throws SQLException {
        return jdbcTemplate.queryForInt("select COUNT(1)  from all_tables WHERE owner= ?  and TABLE_NAME = ?", schema.getName(), name) > 0;
    }

    @Override
    protected void doLock() throws SQLException {

    }
}
